define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'yunfood/marketing/promotion/index' + location.search,
                    add_url: 'yunfood/marketing/promotion/add',
                    edit_url: 'yunfood/marketing/promotion/edit',
                    del_url: 'yunfood/marketing/promotion/del',
                    multi_url: 'yunfood/marketing/promotion/multi',
                    table: 'yunfood_promotion'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'name', title: __('促销名称'), operate: 'LIKE %...%', placeholder: '模糊搜索'},
                        {
                            field: 'status', 
                            title: __('启用状态'), 
                            searchList: {"1": __('开启'), "2": __('关闭')},
                            formatter: Table.api.formatter.status,
                            custom: {"1": 'success', "2": 'danger'}
                        },
                        {
                            field: 'type', 
                            title: __('类型'), 
                            searchList: {"1": __('促销'), "2": __('优惠券'), "3": __('团购'), "4": __('秒杀')},
                            formatter: function (value, row, index) {
                                var types = {"1": __('促销'), "2": __('优惠券'), "3": __('团购'), "4": __('秒杀')};
                                return types[value] || '';
                            }
                        },
                        {field: 'sort', title: __('排序'), sortable: true},
                        {
                            field: 'exclusive', 
                            title: __('排他'), 
                            searchList: {"1": __('不排他'), "2": __('排他')},
                            formatter: function (value, row, index) {
                                var exclusives = {"1": __('不排他'), "2": __('排他')};
                                return exclusives[value] || '';
                            }
                        },
                        {
                            field: 'auto_receive', 
                            title: __('自动领取'), 
                            visible: function (row) {
                                // 只有当类型为优惠券时才显示自动领取字段
                                return row.type == 2;
                            },
                            searchList: {"1": __('自动领取'), "2": __('不自动领取')},
                            formatter: function (value, row, index) {
                                if (row.type != 2) {
                                    return '-';
                                }
                                var autoReceives = {"1": __('自动领取'), "2": __('不自动领取')};
                                return autoReceives[value] || '';
                            }
                        },
                        {
                            field: 'stime', 
                            title: __('开始时间'), 
                            formatter: Table.api.formatter.datetime, 
                            operate: 'RANGE', 
                            addclass: 'datetimerange'
                        },
                        {
                            field: 'etime', 
                            title: __('结束时间'), 
                            formatter: Table.api.formatter.datetime, 
                            operate: 'RANGE', 
                            addclass: 'datetimerange'
                        },
                        {
                            field: 'time_valid', 
                            title: __('时间有效性'), 
                            formatter: function (value, row, index) {
                                var now = Math.floor(new Date().getTime() / 1000);
                                if (row.stime <= now && row.etime >= now) {
                                    return '<span class="label label-success">有效</span>';
                                } else {
                                    return '<span class="label label-danger">无效</span>';
                                }
                            }
                        },
                        {
                            field: 'operate', 
                            title: __('Operate'), 
                            table: table, 
                            events: Table.api.events.operate, 
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                
                // 初始化时间选择器（添加检查确保插件已加载）
                if ($.fn.datetimepicker) {
                    $('.datetimepicker').datetimepicker({
                        format: 'YYYY-MM-DD HH:mm:ss',
                        icons: {
                            time: 'fa fa-clock-o',
                            date: 'fa fa-calendar',
                            up: 'fa fa-chevron-up',
                            down: 'fa fa-chevron-down',
                            previous: 'fa fa-chevron-left',
                            next: 'fa fa-chevron-right',
                            today: 'fa fa-history',
                            clear: 'fa fa-trash',
                            close: 'fa fa-remove'
                        }
                    });
                }
            }
        }
    };
    return Controller;
});